home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cdrecord-1.8.1 / lib / stdio / flag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-05  |  2.8 KB  |  129 lines

  1. /* @(#)flag.c    2.5 98/09/05 Copyright 1986 J. Schilling */
  2. /*
  3.  *    Copyright (c) 1986 J. Schilling
  4.  */
  5. /*
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2, or (at your option)
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; see the file COPYING.  If not, write to
  18.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include "io.h"
  23. #include <stdxlib.h>
  24.  
  25. #ifdef    DO_MYFLAG
  26.  
  27. #define    FL_INIT    10
  28.  
  29. int    _io_glflag;        /* global default flag */
  30. int    _fl_inc = 10;        /* increment for expanding flag struct */
  31. int    _fl_max = FL_INIT;    /* max fd currently in _io_myfl */
  32. _io_fl    _io_smyfl[FL_INIT];    /* initial static space */
  33. _io_fl    *_io_myfl = _io_smyfl;    /* init to static space */
  34.  
  35. LOCAL int _more_flags    __PR((FILE * ));
  36.  
  37. LOCAL int _more_flags(fp)
  38.     FILE    *fp;
  39. {
  40.     register int    f = fileno(fp);
  41.     register int    n = _fl_max;
  42.     register _io_fl    *np;
  43.  
  44.     while (n <= f)
  45.         n += _fl_inc;
  46.  
  47.     if (_io_myfl == _io_smyfl) {
  48.         np = (_io_fl *) malloc(n * sizeof(*np));
  49.         fillbytes(np, n * sizeof(*np), '\0');
  50.         movebytes(_io_smyfl, np, sizeof(_io_smyfl)/sizeof(*np));
  51.     } else {
  52.         np = (_io_fl *) realloc(_io_myfl, n * sizeof(*np));
  53.         if (np)
  54.             fillbytes(&np[_fl_max], (n-_fl_max)*sizeof(*np), '\0');
  55.     }
  56.     if (np) {
  57.         _io_myfl = np;
  58.         _fl_max = n;
  59.         return (_io_get_my_flag(fp));
  60.     } else {
  61.         return (_IONORAISE);
  62.     }
  63. }
  64.  
  65. int _io_get_my_flag(fp)
  66.     register FILE    *fp;
  67. {
  68.     register int    f = fileno(fp);
  69.     register _io_fl    *fl;
  70.  
  71.     if (f >= _fl_max)
  72.         return (_more_flags(fp));
  73.  
  74.     fl = &_io_myfl[f];
  75.  
  76.     if (fl->fl_io == 0 || fl->fl_io == fp)
  77.         return (fl->fl_flags);
  78.  
  79.     while (fl && fl->fl_io != fp)
  80.         fl = fl->fl_next;
  81.  
  82.     if (fl == 0)
  83.         return (0);
  84.  
  85.     return (fl->fl_flags);
  86. }
  87.  
  88. void _io_set_my_flag(fp, flag)
  89.     FILE    *fp;
  90.     int    flag;
  91. {
  92.     register int    f = fileno(fp);
  93.     register _io_fl    *fl;
  94.     register _io_fl    *fl2;
  95.  
  96.     if (f >= _fl_max)
  97.         (void) _more_flags(fp);
  98.  
  99.     fl = &_io_myfl[f];
  100.  
  101.     if (fl->fl_io != (FILE *)0) {
  102.         fl2 = fl;
  103.  
  104.         while (fl && fl->fl_io != fp)
  105.             fl = fl->fl_next;
  106.         if (fl == 0) {
  107.             if ((fl = (_io_fl *) malloc(sizeof(*fl))) == 0)
  108.                 return;
  109.             fl->fl_next = fl2->fl_next;
  110.             fl2->fl_next = fl;
  111.         }
  112.     }
  113.     fl->fl_io = fp;
  114.     fl->fl_flags = flag;
  115. }
  116.  
  117. void _io_add_my_flag(fp, flag)
  118.     FILE    *fp;
  119.     int    flag;
  120. {
  121.     int    oflag = _io_get_my_flag(fp);
  122.  
  123.     oflag |= flag;
  124.  
  125.     _io_set_my_flag(fp, oflag);
  126. }
  127.  
  128. #endif    /* DO_MYFLAG */
  129.